package org.yang.snake.util;

import lombok.experimental.UtilityClass;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.yang.snake.security.SnakeUser;

/**
 * @author shichenyang
 */
@UtilityClass
public class SecurityUtil {
    /**
     * 获取Authentication
     */
    public Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    /**
     *  获取用户
     * @param authentication
     * @return EduUser 成教的扩展用户
     */
    public SnakeUser getUser(Authentication authentication) {
        Object principal = authentication.getPrincipal();
        if (principal instanceof SnakeUser) {
            return (SnakeUser) principal;
        }
        return null;
    }
    /**
     * 获取用户
     */
    public SnakeUser getUser() {
        Authentication authentication = getAuthentication();
        return getUser(authentication);
    }

}
